/******************************************************************************
 * Copyright (C) 2015  671643387@qq.com
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *****************************************************************************/

#ifndef UCOP_DATA_REL_SESSIONIMPL_H_
#define UCOP_DATA_REL_SESSIONIMPL_H_

#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif

#include <cstdarg>
#include "Ucop/Util/String.h"
#include "Ucop/Data/Rel/Session.h"
#include "Ucop/Data/Rel/SqlOperation.h"
#include "Ucop/Data/Rel/SqlDelayThread.h"


#define ASYNC_QUERY_BODY(_Sql) if (!_Sql || !_MyresultQueue.get()) return false;

#define ASYNC_PQUERY_BODY(_Format, _Query)                                                            \
    if (!_Format) { return false; }                                                                   \
    char _Query[MAX_QUERY_LENGTH];                                                                    \
    std::va_list _Ap;                                                                                 \
    va_start(_Ap, _Format);                                                                           \
    int _Res = Util::vsnprintf(_Query, MAX_QUERY_LENGTH, _Format, _Ap);                               \
    va_end(_Ap);                                                                                      \
    if (_Res == -1)                                                                                   \
    {                                                                                                 \
        _MyerrHandler(-1, "SQL Query truncated (and not execute) for format: %s", _Format);           \
        return false;                                                                                 \
    }                                                                                                 \


namespace Ucop {
namespace Data {
namespace Rel {

    template<class Class, typename ParamType1>
	bool Session::AsyncQuery(Class* _Object, void (Class::*_Method)(QueryResult*, ParamType1), ParamType1 _Param1, const char* _Sql)
    {
        ASYNC_QUERY_BODY(_Sql);
        return _MydelayThread->Delay(new SqlQuery(_Sql, new QueryCallback<Class, ParamType1>(_Object, _Method, (QueryResult*)NULL, _Param1), _MyresultQueue.get()));
    }

    template<class Class, typename ParamType1>
    bool Session::AsyncPQuery(Class* _Object, void (Class::*_Method)(QueryResult*, ParamType1), ParamType1 _Param1, const char* _Format, ...)
    {
        ASYNC_PQUERY_BODY(_Format, _Query);
        return AsyncQuery(_Object, _Method, _Param1, _Query);
    }

}}}

#endif // UCOP_DATA_REL_SESSIONIMPL_H_
